package com.aaa.controller.goods;

import com.aaa.entity.ResponseDto;
import com.aaa.service.CategoryService;
import com.aaa.service.impl.CategoryServiceImpl;
import com.aaa.util.IntegerUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/GetCategoryAllServlet")
public class GetCategoryAllServlet extends HttpServlet {
    CategoryService categoryService = null;
    @Override
    public void init() throws ServletException {
        categoryService = new CategoryServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ResponseDto responseDto = new ResponseDto();
        try {
            Integer pageNumber = IntegerUtils.ToInteger(req.getParameter("pageNumber"));
            Integer pageSize = IntegerUtils.ToInteger(req.getParameter("pageSize"));
            String searchCategoryId = req.getParameter("searchCategoryId");
            String searchCategoryName = req.getParameter("searchCategoryName");
            responseDto.setData(categoryService.getAllCategoryList(pageNumber,pageSize,searchCategoryId,searchCategoryName));
            responseDto.setMessage("请求成功");
            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
        }catch (Exception e){
            e.printStackTrace();
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage(e.getMessage());
            responseDto.setMessage("请求失败");
        }
        resp.getWriter().print(new Gson().toJson(responseDto));
    }
}
